import axios from 'axios'
import router from "@/router";

// 创建 axios 实例
const service = axios.create({
    baseURL: import.meta.env.VITE_VUE_APP_SERVER_URL, // api base_url
    timeout: 9000, // 请求超时时间
})

service.interceptors.request.use(config => {
    let userInfo: any = window.localStorage.getItem("userInfo")
    if(userInfo!=null) {
        config.headers.Authorization = JSON.parse(userInfo).token
    }
    return config
})


service.interceptors.response.use(config => {
    if(config.data.code == 401) {
        localStorage.clear()
        router.push("/admin/login");
    }
    return config.data;
})

export {
    service as axios
}